package com.action.app_center;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.enetiy.Useraccount;
import com.enetiy.Users;
import com.service.AuthCodeService;
import com.service.LoginService;
import com.service.UseraccountService;
import com.util.DataUtil;
import com.util.Log4gUtil;

public class UserContentAction {
	private String content;
	private String type;
	private int userid;
	
	private String phone;
	private String authcode;
	private String params;
	private UseraccountService useraccountService;
	private LoginService loginService;
	private AuthCodeService authCodeService;
	/**
	 * 修改用户内容  >Nf027
	 */
	public void updateContent(){
		if(params!=null&&!params.equals("")){
			JSONObject dataParser = DataUtil.dataParser(params);
			if(dataParser.has("content"))
				content = dataParser.getString("content");
			if(dataParser.has("type"))
				type = dataParser.getString("type");
			if(dataParser.has("userid"))
				userid = dataParser.getInt("userid");
		}
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out =null;
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		try {
			out = response.getWriter();
			Users user = loginService.selectUserId(userid);
			if(user!=null){
				if(type.equals("pwd")&&content!=null&&!content.equals(""))
					user.setPwd(content);
				if(type.equals("nickname")&&content!=null&&!content.equals(""))
					user.setNickname(content);
				if(type.equals("sex")&&content!=null&&!content.equals("")){
					if(content.equals("1"))
						user.setSex(1);
					else
						user.setSex(2);
				}
				if(type.equals("selfdom")&&content!=null&&!content.equals(""))
					user.setSelfdom(content);	
				loginService.updateUser(user);
				sendJson.put("code", 1);
			}else{
				sendJson.put("code", -1);
				sendJson.put("msg", "用户不存在");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg", "服务器异常");
			e.printStackTrace();
		}
		out.print(sendJson);
		Log4gUtil.getLog().info("updateContent-userid:"+userid+"输出:"+sendJson.toString());
	}
	/**
	 * 修改手机号码 >Nf028
	 */
	public void updatePhone(){
		if(params!=null&&!params.equals("")){
			JSONObject dataParser = DataUtil.dataParser(params);
			if(dataParser.has("userid"))
				userid = dataParser.getInt("userid");
			if(dataParser.has("phone"))
				phone = dataParser.getString("phone");
			if(dataParser.has("authcode"))
				authcode = dataParser.getString("authcode");
		}
		
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out =null;
		net.sf.json.JSONObject sendJson = DataUtil.sendJson();
		try {
			out = response.getWriter();
			boolean selectAuthCode = authCodeService.selectAuthCode(phone, authcode);
			Useraccount useraccount = useraccountService.selectUseraccount(userid, 1);
			if(selectAuthCode){
				if(useraccount!=null){
					useraccount.setAccount(phone);
					useraccountService.updateUseraccount(useraccount);
					sendJson.put("code", 1);
				}else{
					sendJson.put("code", -1);
					sendJson.put("msg", "用户不存在");
				}
			}else{
				sendJson.put("code", 0);
				sendJson.put("msg", "手机验证码错误");
			}
		} catch (Exception e) {
			sendJson.put("code", -1);
			sendJson.put("msg", "服务器异常");
			e.printStackTrace();
		}
			out.print(sendJson);
			Log4gUtil.getLog().info("updatePhone-userid:"+userid+"输出:"+sendJson.toString());
		}
	
	
	
	public void setAuthCodeService(AuthCodeService authCodeService) {
		this.authCodeService = authCodeService;
	}
	public void setUseraccountService(UseraccountService useraccountService) {
		this.useraccountService = useraccountService;
	}

	public void setContent(String content) {
		this.content = content;
	}
	public void setType(String type) {
		this.type = type;
	}
	public void setUserid(int userid) {
		this.userid = userid;
	}
	public void setParams(String params) {
		this.params = params;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public void setAuthcode(String authcode) {
		this.authcode = authcode;
	}
	
}
